﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OTPlus.Core.Content
{
    public static class EmbedImgur
    {
        public static string Embed(string content)
        {
            Int32 imgTagStart = 0;
            Int32 tagStart = 0;
            Int32 tagEnd = 0;
            Int32 imgCount = 0;

            //replace "<a href" with "<img src" across the entire string
            content = content.Replace("<a href=\"http://i.imgur.com", "<img src=\"http://i.imgur.com");

            //determine how many images are in post
            imgCount = (content.Length - content.Replace("<img src", "").Length) / "<img src".Length;

            //loop through and clean unwanted text
            tagStart = 0;
            for (int i = 0; i < imgCount; i++)
            {
                //find start/end position for img
                imgTagStart = content.IndexOf("<img src", tagStart);
                tagStart = content.IndexOf(">", imgTagStart + 1);
                tagEnd = content.IndexOf(">", tagStart + 1);

                //clean up content only keeping embed tag
                content = content.Remove(tagStart, tagEnd - tagStart);
                tagStart = imgTagStart + 1;
            }

            return content;
        }
    }



    public static class EmbedYoutube
    {

        //clusterF of string manipulation, index counting, substrings, etc
        //proceed at your own risk!
        public static string Embed(string content)
        {
            Int32 ytTagStart = 0;
            Int32 ytTagEnd = 0;
            string ytValue = string.Empty;

            Int32 ytCount = 0;
            string ytEmbed = string.Empty;

            //determine how many YouTube Videos are in post
            ytCount = (content.Length - content.Replace("<a href=\"http://www.youtube.com", "").Length) / "<a href=\"http://www.youtube.com".Length;
            
            //Loop through each video and replace with embed code
            for (int i = 0; i < ytCount; i++)
            {
                content = content.Replace("http://www.youtube.com/watch?v=", "http://www.youtube.com/v/");

                ytTagStart = content.IndexOf("<a href=\"http://www.youtube.com", 0) + 9;
                if (ytTagStart > 8)     //if less than 9, means IndexOf returned -1
                {
                    ytTagEnd = content.IndexOf(">", ytTagStart);
                    ytValue = content.Substring(ytTagStart, ytTagEnd - ytTagStart - 2);

                    //add video to embed code
                    ytEmbed = "<object width='425' height='355'><param name='movie' value='" + ytValue + "'></param>" +
                              "<param name='wmode' value='transparent'></param><embed src='" + ytValue + "'" +
                              "type='application/x-shockwave-flash' wmode='transparent' width='425' height='355'></embed></object>";

                    //end of yt tag including video title
                    ytTagEnd = content.IndexOf("</a>", ytTagStart);

                    //replace original code in content string
                    content = content.Replace(content.Substring(ytTagStart - 9, ytTagEnd - ytTagStart + 13), ytEmbed);
                }
            }

            return content;
        }
    }

}